﻿/*
创建人：zhunan
日期：2022.8.9
描述：热表重点转工批次进度维护js
*/
var page = {
  pagetotal: 0,
  pageNumber: 1,
  pageSize: 8,
  dialog: {
    pagetotal: 0,
    pageNumber: 1,
    pageSize: 8
  },
  mainUrl: getRootPath() + "/Ashx/ReBiaoConainer/RbFocusContainerProcessHandler.ashx", //本页面后台路径
  //初始化页面
  init: function () {
    page.bind();
    page.initData(); //初始化页面数据
  },
  // 页面事件绑定
  bind: function () {
    // 重点批次进度表格
    $("#tbGrid").datagrid({
      columns: [[
        { field: 'ck', title: '选择', checkbox: true },
        {
          field: 'sourcetype', title: '数据来源', width: 60,
          formatter: function (value) {
            if (value === 1) {
              return 'MES';
            } else {
              return "其他";
            }
          }
        },
        { field: 'fromfactoryname', title: '主制分厂', width: 120 },
        { field: 'tofactoryname', title: '承制分厂', hidden: true },
        { field: 'containername', title: '批次号', width: 180 },
        { field: 'qty', title: '数量', width: 40 },
        { field: 'workflowstepname', title: '工序', width: 100 },
        { field: 'currentprocess', title: '加工进度', width: 140 },
        {
          field: 'status', title: '完成状态', width: 60,
          styler: function (value, row, index) {
            if (value === '未完成') {
              return 'color:red;';
            } else {
              return 'color:green;';
            }
          }
        },
        {
          field: 'toacceptdate', title: '接收时间', width: 90,
          formatter: function (value) {
            if (value) {
              return value.split('T')[0];
            } else {
              return "";
            }
          }
        },
        {
          field: 'needcompletedate', title: '要求完成时间', width: 90,
          formatter: function (value) {
            if (value) {
              return value.split('T')[0];
            } else {
              return "";
            }
          }
        },
        {
          field: 'completedate', title: '完成时间', width: 90,
          formatter: function (value) {
            if (value) {
              return value.split('T')[0];
            } else {
              return "";
            }
          }
        },
        { field: 'productname', title: '零件图号', width: 80 },
        { field: 'productdescription', title: '零件名称', width: 90 },
        { field: 'createemployee', title: '添加人', width: 60 },
        {
          field: 'createdate', title: '添加时间', width: 90,
          formatter: function (value) {
            if (value) {
              return value.split('T')[0];
            } else {
              return "";
            }
          }
        },
        { field: 'modifyemployee', title: '修改人', width: 60 },
        {
          field: 'modifydate', title: '修改时间', width: 90,
          formatter: function (value) {
            if (value) {
              return value.split('T')[0];
            } else {
              return "";
            }
          }
        },
        { field: 'rbfocuscontainerprocessid', title: 'rbfocuscontainerprocessid', hidden: true }
      ]],
      onLoadSuccess: function (data) {
        $('#pp').pagination('refresh', {	// 改变选项，并刷新分页栏信息
          total: page.pagetotal,
          pageSize: page.pageSize,
          pageNumber: page.pageNumber
        });
        customizedEasyuiPagination('pp'); //定制化分页
      },
      onCheck: function (rowIndex, rowData) {
        $("#txtProcess").textbox('setValue', rowData.currentprocess);
      }
    });

    // 对话框表格
    $("#tbDialogGrid").datagrid({
      columns: [[
        { field: 'ck', title: '选择', checkbox: true },
        { field: 'fromfactoryname', title: '主制分厂', width: 140 },
        { field: 'productname', title: '零件图号', width: 80 },
        { field: 'productdescription', title: '零件名称', width: 90 },
        { field: 'containername', title: '批次号', width: 200 },
        { field: 'qty', title: '数量', width: 40 },
        { field: 'workflowstepname', title: '工序名称', width: 120 },
        {
          field: 'toacceptdate', title: '接收时间', width: 100,
          formatter: function (value, row, index) {
            if (value) {
              return value.split('T')[0];
            } else {
              return "";
            }
          }
        },
        {
          field: 'needcompletedate', title: '要求完成时间', width: 100,
          formatter: function (value, row, index) {
            if (value) {
              return value.split('T')[0];
            } else {
              return "";
            }
          }
        },
        { field: 'rbfocuscontainerprocessid', title: 'rbfocuscontainerprocessid', hidden: true }
      ]],
      onLoadSuccess: function (data) {
        $('#ppDialog').pagination('refresh', {	// 改变选项，并刷新分页栏信息
          total: page.dialog.pagetotal,
          pageSize: page.dialog.pageSize,
          pageNumber: page.dialog.pageNumber
        });
        customizedEasyuiPagination('ppDialog'); //定制化分页
      },
      onDblClickCell: function (index, field, value) {
        $(this).datagrid('beginEdit', index);
        var ed = $(this).datagrid('getEditor', { index: index, field: ['needcompletedate'] });
        $(ed.target).focus();
        $(this).datagrid('checkRow', index);
      },
      onClickRow: function (index) {
        $(this).datagrid('acceptChanges');
      }
    });

    //分页事件
    $('#pp').pagination({
      onSelectPage: function (pageNumber, pageSize) {
        customizedEasyuiPagination('pp'); //定制化分页
        page.pageSize = pageSize;
        page.pageNumber = pageNumber;
        page.search();
      }
    });

    // 弹出框--分页事件
    $('#ppDialog').pagination({
      onSelectPage: function (pageNumber, pageSize) {
        customizedEasyuiPagination('ppDialog'); //定制化分页
        page.dialog.pageSize = pageSize;
        page.dialog.pageNumber = pageNumber;
        page.dialogSearch();
      }
    });

    // 查询按钮
    $("#btnSearch").on("click", function () {
      page.pageNumber = 1;
      page.search();
    });

    // 重置
    $("#btnReset").on('click', function () {
      page.resetData();
    });

    //添加非MES数据
    $("#btnDirectAdd").on("click", function () {
      var data = page.validate();
      if (data.length !== 0) {
        page.insertNotMESData(data);
      }
    });

    // 增加(MES数据)按钮
    $("#btnAdd").on("click", function () {
      $('#dialog').window('open');
    });

    // 修改按钮
    $("#btnUpdate").on("click", function () {
      var datas = $("#tbGrid").datagrid('getChecked');
      var processText = $("#txtProcess").val();
      if (datas.length === 0) {
        displayMessage("请勾选需要修改的记录！", false);
        return;
      }
      if (!processText) {
        displayMessage("加工进展不能为空！", false);
        return;
      }
      page.updateData();
    });

    // 删除按钮
    $("#btnDelete").on("click", function () {
      var datas = $("#tbGrid").datagrid('getChecked');
      if (datas.length === 0) {
        displayMessage("请勾选需要删除的记录！", false);
        return;
      }
      $.messager.confirm('删除前确认', '你确定要删除吗?', function (r) {
        // 确认删除
        if (r) {
          page.deleteData(datas);
        }
      });
    });

    // 弹出框--查询按钮
    $("#btnDialogSearch").on("click", function () {
      page.dialog.pageNumber = 1;
      page.dialogSearch();
    });

    // 弹出框--重置按钮
    $("#btnDialogReset").on("click", function () {
      page.dialogReset();
    });

    // 弹出框--保存按钮
    $("#btnDialogSave").on("click", function () {
      var datas = $("#tbDialogGrid").datagrid('getChecked');
      if (datas.length === 0) {
        $("#dialogMessage").text('请勾选需要保存的批次！').css('color', 'red');
        return;
      }
      page.insertData();
    });

    // 关闭弹窗
    $("#btnDialogClose").on("click", function () {
      $('#dialog').window('close');
      page.dialogReset();
    });
  },
  // 初始化页面
  initData: function () {
    page.getFactory();
  },
  // 查询
  search: function () {
    var formData = {
      action: "getData",
      pageSize: page.pageSize,
      pageNumber: page.pageNumber,
      factoryname: $('#ddlFactory').combobox('getText'),
      containername: $("#txtContainername").textbox('getValue'),
      productname: $("#txtProductname").textbox('getValue'),
      status: $('#ddlStatus').combobox('getValue'),
      recivedateStart: $("#txtToAcceptDateStart").datebox('getValue'),
      recivedateEnd: $("#txtToAcceptDateEnd").datebox('getValue'),
      sourcetype: $('#ddlSourceType').combobox('getValue')
    };

    $.ajax({
      type: "Post",
      url: page.mainUrl + "?actions=getData",
      data: formData,
      cashe: false,
      async: true,
      beforeSend: function () {
        $('#tbGrid').datagrid('loading');
        $(this).attr("disabled", "disabled"); //禁用按钮
      },
      success: function (data) {
        $('#tbGrid').datagrid('loaded');
        $(this).removeAttr("disabled"); //恢复按钮
        var result = JSON.parse(data);
        if (result.Result) {
          //给分页对象赋值
          page.pagetotal = result.RowCount;
          $('#tbGrid').datagrid('loadData', { total: result.RowCount, rows: result.DBTable });
        } else {
          displayMessage(result.Message, false);
        }
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
        $('#tbGrid').datagrid('loaded');
        $(this).removeAttr("disabled"); //恢复按钮
        alert(XMLHttpRequest.responseText);
      }
    });
  },
  // 更新
  updateData: function () {
    var selectData = $("#tbGrid").datagrid('getChecked');
    var finishdate = "";
    if (!isNaN(dayjs($("#txtInputFinishDate").datebox('getValue')).$y)) {
      finishdate = $("#txtInputFinishDate").datebox('getValue');
    }
    var needfinishdate = "";
    if (!isNaN(dayjs($("#txtInputNeedfinishDate").datebox('getValue')).$y)) {
      needfinishdate = $("#txtInputNeedfinishDate").datebox('getValue');
    }
    var data = {
      action: "updateData",
      processText: $("#txtProcess").val(),
      selectData: JSON.stringify(selectData),
      finishdate: finishdate,
      needfinishdate: needfinishdate
    };
    $.ajax({
      type: "post",
      url: page.mainUrl + "?actions=updateData",
      data: data,
      cashe: false,
      dataType: 'json',
      success: function (res) {
        if (res.Result) {
          displayMessage('修改成功', true);
          $('#tbGrid').datagrid('loadData', []);
          $("#txtProcess").textbox('setValue', '');
          page.dialogSearch(1);
        } else {
          displayMessage(res.Message, false);
        }
      }
    });
  },
  // 弹出框 表格查询
  dialogSearch: function () {
    $('#tbDialogGrid').datagrid('loadData', []);
    var formData = {
      action: "GetAcceptedContainerShipData",
      pageSize: page.dialog.pageSize,
      pageNumber: page.dialog.pageNumber,
      factoryname: $('#ddlDialogFactory').combobox('getText'),
      containername: $("#txtDialogContainername").textbox('getValue'),
      productname: $("#txtDialogProductname").textbox('getValue'),
      recivedateStart: $("#txtDialogToAcceptDateStart").datebox('getValue'),
      recivedateEnd: $("#txtDialogToAcceptDateEnd").datebox('getValue'),
    };

    $.ajax({
      type: "Post",
      url: page.mainUrl + "?actions=GetAcceptedContainerShipData",
      data: formData,
      cashe: false,
      async: true,
      beforeSend: function () {
        $('#tbDialogGrid').datagrid('loading');
        $(this).attr("disabled", "disabled"); //禁用按钮
      },
      success: function (data) {
        $('#tbDialogGrid').datagrid('loaded');
        $(this).removeAttr("disabled"); //恢复按钮
        var result = JSON.parse(data);
        if (result.Result) {
          //给分页对象赋值
          page.dialog.pagetotal = result.RowCount;
          $('#tbDialogGrid').datagrid('loadData', { total: result.RowCount, rows: result.DBTable });
        } else {
          $("#dialogMessage").text('消息：' + result.Message).css('color', 'red');;
        }
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
        $('#tbDialogGrid').datagrid('loaded');
        $(this).removeAttr("disabled"); //恢复按钮
        alert(XMLHttpRequest.responseText);
      }
    });
  },
  // 弹出框--保存
  insertData: function () {
    $("#tbDialogGrid").datagrid("acceptChanges");
    var selectData = $("#tbDialogGrid").datagrid('getChecked');
    var data = {
      action: "insertData",
      processText: $("#txtDialogProcess").val(),
      sourcetype: 1,
      selectData: JSON.stringify(selectData)
    };
    $.ajax({
      type: "post",
      url: page.mainUrl + "?actions=insertData",
      data: data,
      cashe: false,
      dataType: 'json',
      success: function (res) {
        if (res.Result) {
          $("#dialogMessage").text('消息：保存成功');
          $('#tbDialogGrid').datagrid('loadData', []);
          $("#txtDialogProcess").textbox('setValue', '');
          page.dialogSearch(1);
        } else {
          $("#dialogMessage").text('消息：' + res.Message).css('color', 'red');
        }
      }
    });
  },
  // 删除
  deleteData: function (data) {
    var data = {
      action: "deleteData",
      willBeDeleteData: JSON.stringify(data)
    };
    $.ajax({
      type: "post",
      url: page.mainUrl + "?actions=deleteData",
      data: data,
      cashe: false,
      dataType: 'json',
      success: function (res) {
        if (res.Result) {
          displayMessage('删除成功！', true);
          $('#tbGrid').datagrid('loadData', []);
          page.dialogSearch(1);
        } else {
          displayMessage(res.Message, false);
        }
      }
    });
  },
  // 获取分厂列表
  getFactory: function () {
    $.ajax({
      type: "post",
      url: getRootPath() + "/Ashx/ApsHandler/EmployeeMaintPage.ashx?actions=getFactory",
      data: { action: "GetFactory" },
      cashe: false,
      dataType: 'json',
      success: function (res) {
        if (res.Result) {
          //绑定分厂
          res.DBTable.unshift({ factoryid: '', factoryname: '' });
          $("#ddlDialogFactory").combobox({
            valueField: 'factoryid',
            textField: 'factoryname',
            panelHeight: 200,
            data: res.DBTable
          });
          // 绑定弹出框分厂下拉框
          $("#ddlFactory").combobox({
            valueField: 'factoryid',
            textField: 'factoryname',
            panelHeight: 200,
            data: res.DBTable
          });
        }
      }
    });
  },
  // 弹出框--页面重置
  dialogReset: function () {
    $('#ddlDialogFactory').combobox('select', '');
    $("#txtDialogProcess").textbox('clear');
    $("#txtDialogContainername").textbox('clear');
    $("#txtDialogProductname").textbox('clear');
    $("#txtDialogToAcceptDateStart").datebox('clear');
    $("#txtDialogToAcceptDateEnd").datebox('clear');
    $("#dialogMessage").text('');
    $("#tbDialogGrid").datagrid('loadData', { total: 0, rows: [] });
    page.dialog.pageNumber = 1;
  },
  // 重置
  resetData: function () {
    $('#ddlFactory').combobox('select', '');
    $("#txtContainername").textbox('clear');
    $("#txtProductname").textbox('clear');
    $("#txtToAcceptDateStart").datebox('clear');
    $("#txtToAcceptDateEnd").datebox('clear');
    $('#ddlStatus').combobox('select', '全部');
    $('#ddlSourceType').combobox('select', '全部');
    $("#txtInputFactoryname").textbox('clear');
    $("#txtInputContainername").textbox('clear');
    $("#txtQty").textbox('clear');
    $("#txtInputWorkflowStep").textbox('clear');
    $("#txtInputProduct").textbox('clear');
    $("#txtInputProductDesc").textbox('clear');
    $("#txtInputReceiveDate").datebox('clear');//接收时间
    $("#txtInputNeedfinishDate").datebox('clear');//要求完成时间
    $("#txtInputFinishDate").datebox('clear');
    $("#txtProcess").textbox('clear');
    $("#tbGrid").datagrid('loadData', { total: 0, rows: [] });
    page.pageNumber = 1;
  },
  // 添加非MES数据前校验
  validate: function () {
    var factoryname = $("#txtInputFactoryname").textbox('getValue');//主制分厂
    var containername = $("#txtInputContainername").textbox('getValue');//批次号
    var qty = $("#txtQty").textbox('getValue');//批次数量
    var workflowstepname = $("#txtInputWorkflowStep").textbox('getValue');//工序
    var productname = $("#txtInputProduct").textbox('getValue');//零件图号
    var productdesc = $("#txtInputProductDesc").textbox('getValue');//零件名称
    var receivedate = $("#txtInputReceiveDate").datebox('getValue');//接收时间
    var needfinishdate = $("#txtInputNeedfinishDate").datebox('getValue');//要求完成时间
    var finishdate = $("#txtInputFinishDate").datebox('getValue');
    var currentprocess = $("#txtProcess").textbox('getValue');
    if (!containername) {
      displayMessage("批次号不能为空！", false);
      return [];
    }
    if (!/^[1-9]\d*$/.test(qty)) {
      displayMessage("数量为空或格式不正确！", false);
      return [];
    }
    if (!workflowstepname) {
      displayMessage("工序内容不能为空！", false);
      return [];
    }
    if (!productname) {
      displayMessage("零件号不能为空！", false);
      return [];
    }
    if (isNaN(dayjs(receivedate).$y)) {
      displayMessage("接收时间为空或格式不正确！", false);
      return [];
    }
    if (isNaN(dayjs(needfinishdate).$y)) {
      displayMessage("要求完成时间为空或格式不正确！", false);
      return [];
    }
    var data = [{
      fromfactoryname: factoryname,
      containername: containername,
      qty: qty,
      workflowstepname: workflowstepname,
      productname: productname,
      productdescription: productdesc,
      toacceptdate: receivedate,
      needcompletedate: needfinishdate,
      finishdate: finishdate,
      currentprocess: currentprocess,
      sourcetype: 2
    }];
    return data;
  },
  // 保存非MES数据
  insertNotMESData: function (postData) {
    var data = {
      action: "insertNotMESData",
      postData: JSON.stringify(postData)
    };
    $.ajax({
      type: "post",
      url: page.mainUrl + "?actions=insertNotMESData",
      data: data,
      cashe: false,
      dataType: 'json',
      success: function (res) {
        if (res.Result) {
          displayMessage('保存成功！', true);
          page.resetData();
        } else {
          displayMessage(res.Message, false);
        }
      }
    });
  }
};

page.init();